package com.yy.wga.user.service.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yy.wga.common.core.dto.Result;
import com.yy.wga.common.core.enmus.EUserGrade;
import com.yy.wga.user.feign.api.UserSummaryFeign;
import com.yy.wga.user.feign.response.UserSummaryResp;
import com.yy.wga.user.service.pojo.UserBaseDO;
import com.yy.wga.user.service.pojo.UserRelationDO;
import com.yy.wga.user.service.pojo.UserSummaryDO;
import com.yy.wga.user.service.service.UserBaseService;
import com.yy.wga.user.service.service.UserRelationService;
import com.yy.wga.user.service.service.UserSummaryService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 用户数据统计
 *
 * @author guochao
 * @since 2024-12-10 22:41:43
 */
@RestController
@RequestMapping(value = "/api/user/summary/")
public class UserSummaryController implements UserSummaryFeign {
    @Resource
    private UserSummaryService userSummaryService;
    @Resource
    private UserRelationService userRelationService;
    @Resource
    private UserBaseService userBaseService;

    @Override
    @GetMapping("get")
    public Result<UserSummaryResp> get(@RequestParam("userId") Long userId) {
        UserBaseDO userBaseDO = userBaseService.getById(userId);
        if (userBaseDO == null) {
            return Result.fail("用户不存在");
        }
        UserSummaryResp response = new UserSummaryResp();
        // v5享客以下，都是看销售额
        EUserGrade userGrade = EUserGrade.getByType(userBaseDO.getGrade(), userBaseDO.getLevel());
        if (userGrade.getType().equals(EUserGrade.SHARE_5.getType())
                && userGrade.getLevel() < EUserGrade.SHARE_5.getLevel()) {
            UserSummaryDO userSummaryDO = userSummaryService.getOne(new LambdaQueryWrapper<UserSummaryDO>()
                    .eq(UserSummaryDO::getAppKey, userBaseDO.getAppKey())
                    .eq(UserSummaryDO::getUserId, userBaseDO.getId()));
            if (userSummaryDO != null) {
                response.setCurrentValue(userSummaryDO.getSaleAmount());
            }
            response.setThresholdType(1);
            response.setThresholdValue(userGrade.getNextGrade().getSalesThreshold());
        } else if (userGrade.getType().equals(EUserGrade.SHARE_5.getType())
                && userGrade.getLevel().equals(EUserGrade.SHARE_5.getLevel())) {
            // v5享客升级到v1服务商,需要推荐10个v5享客
            long v5ShareCount = userRelationService.count(new LambdaQueryWrapper<UserRelationDO>()
                    .eq(UserRelationDO::getAppKey, userBaseDO.getAppKey())
                    .eq(UserRelationDO::getParentUserId, userBaseDO.getId())
                    .eq(UserRelationDO::getGrade, EUserGrade.SHARE_5.getType())
                    .eq(UserRelationDO::getLevel, EUserGrade.SHARE_5.getLevel()));
            response.setThresholdType(2);
            response.setCurrentValue((int) v5ShareCount);
            response.setThresholdValue(10);
        } else {
            // v1服务商升级到v2服务商，需要5个服务商
            long serverCount = userRelationService.count(new LambdaQueryWrapper<UserRelationDO>()
                    .eq(UserRelationDO::getAppKey, userBaseDO.getAppKey())
                    .eq(UserRelationDO::getParentUserId, userBaseDO.getId())
                    .eq(UserRelationDO::getGrade, EUserGrade.SERVER_1.getType()));
            response.setThresholdType(2);
            response.setCurrentValue((int) serverCount);
            response.setThresholdValue(5);
        }
        return Result.success(response);
    }
}
